----------------------------------------------
-- infMonk
----------------------------------------------

local _, enClass = UnitClass("player")
if enClass ~= "MONK" then return end

infMonk = LibStub("AceAddon-3.0"):NewAddon("infMonk", "AceEvent-3.0", "AceBucket-3.0")

local pairs = pairs
local IsShown = IsShown
local UnitBuff = UnitBuff
local SetCooldown = SetCooldown
local GetSpellInfo = GetSpellInfo
local ActionButton_ShowOverlayGlow = ActionButton_ShowOverlayGlow
local ActionButton_HideOverlayGlow = ActionButton_HideOverlayGlow

local player_buffs = {
	[125359] = false, --tiger power
	[139597] = false, --muscle memory
	[118674] = false, --vital mists
	[125195] = false, --tigereye brew
	[116740] = false, --tigereye brew (the damage increase)
	[115294] = false, --mana tea
	[134563] = false, --healing elixir
	[137639] = false, --storm, earth, and fire
}

function infMonk:OnInitialize()
	self:FrameCreation()

	self:RegisterBucketEvent("UNIT_AURA", 0.1, "UNIT_AURA")
	self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
end

function infMonk:FrameCreation()
	local size = 40	
	local backdrop = {
		bgFile = nil, 
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		edgeSize = 12,
		tile = false,
		insets = {left = 1, right = 1, top = 1, bottom = 1},
	}

	--Tiger Power
	palm_icon = CreateFrame("Frame", nil, UIParent)
	palm_icon:SetSize(size, size)
	palm_icon:SetPoint("CENTER", UIParent, "CENTER", 0, -82)
	palm_icon.cdtexture = CreateFrame("Cooldown", nil, palm_icon)
	palm_icon.cdtexture:SetReverse(true)
	palm_icon.cdtexture:SetAllPoints(true)
	palm_icon.cdtexture:SetFrameLevel(1)
	palm_icon:Hide()

	palm_texture = palm_icon:CreateTexture(nil, "BACKGROUND")
	palm_texture:SetAllPoints(true)
	palm_texture:SetTexture("Interface\\Icons\\Ability_Monk_TigerPalm")

	palm_border = CreateFrame("Frame", nil, palm_icon)
	palm_border:SetAllPoints(true)
    palm_border:SetBackdrop(backdrop)

	--Muscle Memory
	mm_icon = CreateFrame("Frame", nil, UIParent)
	mm_icon:SetSize(size, size)
	mm_icon:SetPoint("CENTER", UIParent, "CENTER", -49, -82)
	mm_icon.cdtexture = CreateFrame("Cooldown", nil, mm_icon)
	mm_icon.cdtexture:SetReverse(true)
	mm_icon.cdtexture:SetAllPoints(true)
	mm_icon.cdtexture:SetFrameLevel(1)
	mm_icon:Hide()

	mm_texture = mm_icon:CreateTexture(nil, "BACKGROUND")
	mm_texture:SetAllPoints(true)
	mm_texture:SetTexture("Interface\\Icons\\Spell_Arcane_MindMastery")	

	mm_border = CreateFrame("Frame", nil, mm_icon)
	mm_border:SetAllPoints(true)
    mm_border:SetBackdrop(backdrop)

	--Vital Mists
	vm_icon = CreateFrame("Frame", nil, UIParent)
	vm_icon:SetSize(size, size)
	vm_icon:SetPoint("CENTER", UIParent, "CENTER", 49, -82)
	vm_icon.cdtexture = CreateFrame("Cooldown", nil, vm_icon)
	vm_icon.cdtexture:SetReverse(true)
	vm_icon.cdtexture:SetAllPoints(true)
	vm_icon.cdtexture.noCooldownCount = true
	vm_icon.cdtexture:SetFrameLevel(1)
	vm_icon:Hide()

	vm_texture = vm_icon:CreateTexture(nil, "BACKGROUND")
	vm_texture:SetAllPoints(true)
	vm_texture:SetTexture("Interface\\Icons\\Ability_Shaman_RepulsionTotem")
	
	vm_overlay = CreateFrame("Frame", nil, vm_icon)
	vm_overlay:ClearAllPoints()
	vm_overlay:SetPoint("TOPLEFT", -3, 3)
	vm_overlay:SetPoint("BOTTOMRIGHT", 3, -3)

	vm_border = CreateFrame("Frame", nil, vm_icon)
	vm_border:SetAllPoints(true)
	vm_border:SetBackdrop(backdrop)

	vm_text = vm_icon.cdtexture:CreateFontString(nil, "OVERLAY")
	vm_text:SetAllPoints(true)
	vm_text:SetFont(STANDARD_TEXT_FONT, 27, "OUTLINE")
	vm_text:SetShadowColor(0, 0, 0, 0.8)
	vm_text:SetShadowOffset(2, -2)
	vm_text:SetTextColor(1,1,0)
	vm_text:SetText("")

	--Tigereye Brew
	brew_icon = CreateFrame("Frame", nil, UIParent)
	brew_icon:SetSize(size, size)
	brew_icon:SetPoint("CENTER", UIParent, "CENTER", 100, -82)
	brew_icon.cdtexture = CreateFrame("Cooldown", nil, brew_icon)
	brew_icon.cdtexture:SetReverse(true)
	brew_icon.cdtexture:SetAllPoints(true)
	brew_icon.cdtexture.noCooldownCount = true
	brew_icon.cdtexture:SetFrameLevel(1)
	brew_icon:Hide()

	brew_texture = brew_icon:CreateTexture(nil, "BACKGROUND")
	brew_texture:SetAllPoints(true)
	brew_texture:SetTexture("Interface\\Icons\\Ability_Monk_TigerEyeBrandy")

	brew_overlay = CreateFrame("Frame", nil, brew_icon)
	brew_overlay:ClearAllPoints()
	brew_overlay:SetPoint("TOPLEFT", -3, 3)
	brew_overlay:SetPoint("BOTTOMRIGHT", 3, -3)

	brew_border = CreateFrame("Frame", nil, brew_icon)
	brew_border:SetAllPoints(true)
	brew_border:SetBackdrop(backdrop)

	brew_text = brew_icon.cdtexture:CreateFontString(nil, "OVERLAY")
	brew_text:SetAllPoints(true)
	brew_text:SetFont(STANDARD_TEXT_FONT, 27, "OUTLINE")
	brew_text:SetShadowColor(0, 0, 0, 0.8)
	brew_text:SetShadowOffset(2, -2)
	brew_text:SetTextColor(1,1,0)
	brew_text:SetText("")

	--Mana Tea
	tea_icon = CreateFrame("Frame", nil, UIParent)
	tea_icon:SetSize(size, size)
	tea_icon:SetPoint("CENTER", UIParent, "CENTER", 100, -82)
	tea_icon.cdtexture = CreateFrame("Cooldown", nil, tea_icon)
	tea_icon.cdtexture:SetReverse(true)
	tea_icon.cdtexture:SetAllPoints(true)
	tea_icon.cdtexture.noCooldownCount = true
	tea_icon.cdtexture:SetFrameLevel(1)
	tea_icon:Hide()

	tea_texture = tea_icon:CreateTexture(nil, "BACKGROUND")
	tea_texture:SetAllPoints(true)
	tea_texture:SetTexture("Interface\\Icons\\Monk_Ability_CherryManaTea")
	
	tea_overlay = CreateFrame("Frame", nil, tea_icon)
	tea_overlay:ClearAllPoints()
	tea_overlay:SetPoint("TOPLEFT", -3, 3)
	tea_overlay:SetPoint("BOTTOMRIGHT", 3, -3)

	tea_border = CreateFrame("Frame", nil, tea_icon)
	tea_border:SetAllPoints(true)
	tea_border:SetBackdrop(backdrop)

	tea_text = tea_icon.cdtexture:CreateFontString(nil, "OVERLAY")
	tea_text:SetAllPoints(true)
	tea_text:SetFont(STANDARD_TEXT_FONT, 27, "OUTLINE")
	tea_text:SetShadowColor(0, 0, 0, 0.8)
	tea_text:SetShadowOffset(2, -2)
	tea_text:SetTextColor(1,1,0)
	tea_text:SetText("")

	--Healing Elixirs
	he_icon = CreateFrame("Frame", nil, UIParent)
	he_icon:SetSize(size, size)
	he_icon:SetPoint("CENTER", UIParent, "CENTER", -100, -82)
	he_icon:Hide()

	he_texture = he_icon:CreateTexture(nil, "BACKGROUND")
	he_texture:SetAllPoints(true)
	he_texture:SetTexture("Interface\\Icons\\Ability_Monk_JasmineForceTea")

	he_border = CreateFrame("Frame", nil, he_icon)
	he_border:SetAllPoints(true)
    he_border:SetBackdrop(backdrop)
	
	--Storm, Earth, and Fire
	sef_icon = CreateFrame("Frame", nil, UIParent)
	sef_icon:SetSize(size, size)
	sef_icon:SetPoint("CENTER", UIParent, "CENTER", -49, -82)
	sef_icon:Hide()

	sef_texture = sef_icon:CreateTexture(nil, "BACKGROUND")
	sef_texture:SetAllPoints(true)
	sef_texture:SetTexture("Interface\\Icons\\Spell_Sandstorm")

	sef_border = CreateFrame("Frame", nil, sef_icon)
	sef_border:SetAllPoints(true)
	sef_border:SetBackdrop(backdrop)
	
	sef_text = sef_icon:CreateFontString(nil, "OVERLAY")
	sef_text:SetAllPoints(true)
	sef_text:SetFont(STANDARD_TEXT_FONT, 27, "OUTLINE")
	sef_text:SetShadowColor(0, 0, 0, 0.8)
	sef_text:SetShadowOffset(2, -2)
	sef_text:SetTextColor(1,1,0)
	sef_text:SetText("")	
end

function infMonk:ACTIVE_TALENT_GROUP_CHANGED()
	if mm_icon:IsShown() then
		self:Clear(139597) --muscle memory
	end
	if brew_icon:IsShown() then
		self:Clear(125195) --tigereye brew
	end
	if tea_icon:IsShown() then
		self:Clear(115294) --mana tea
	end
	if sef_icon:IsShown() then
		self:Clear(137639) --storm, earth, and fire
	end	
end

function infMonk:UNIT_AURA(units)
	for unitId in pairs(units) do
		self:BucketUpdate(unitId)
	end
end

function infMonk:BucketUpdate(unitId)
	if unitId ~= "player" then return end
	for id in pairs(player_buffs) do
		local name = GetSpellInfo(id)
		local _, _, icon, count, _, duration, expirationTime = UnitBuff(unitId, name)
		if icon then
			self:MonkTracker(name, id, count, duration, expirationTime, unitId)
			if not player_buffs[id] then
				player_buffs[id] = true
			end
		else
			if player_buffs[id] then
				player_buffs[id] = false
				self:Clear(id)
			end
		end
	end
end

function infMonk:MonkTracker(name, id, count, duration, expirationTime, unitId)
	if id == 125359 then --tiger power
		if expirationTime > 0 then
			local startTime = expirationTime - duration
			palm_icon.cdtexture:SetCooldown(startTime, duration)
			palm_icon.cdtexture:Show()
			palm_icon:Show()
		end
	elseif id == 139597 then --muscle memory
		if expirationTime > 0 then
			local startTime = expirationTime - duration
			mm_icon.cdtexture:SetCooldown(startTime, duration)
			mm_icon.cdtexture:Show()
			mm_icon:Show()
		end
	elseif id == 118674 then --vital mists
		if expirationTime > 0 then
			local startTime = expirationTime - duration
			vm_icon.cdtexture:SetCooldown(startTime, duration)
			vm_icon.cdtexture:Show()
			vm_icon:Show()
		end
		if count == 5 then
			ActionButton_ShowOverlayGlow(vm_overlay)
		else
			ActionButton_HideOverlayGlow(vm_overlay)
		end
		vm_text:SetText(count)		
	elseif id == 125195 then --tigereye brew
		if expirationTime > 0 then
			local startTime = expirationTime - duration
			brew_icon.cdtexture:SetCooldown(startTime, duration)
			brew_icon.cdtexture:Show()
			brew_icon:Show()
		end
		if count == 10 then
			ActionButton_ShowOverlayGlow(brew_overlay)
		end
		brew_text:SetText(count)
	elseif id == 116740 then --tigereye brew (the damage increase)
		local _, _, _, amount, _, _, _, _, _, _, _ = UnitBuff(unitId, GetSpellInfo(125195))
		if amount < 10 then
			ActionButton_HideOverlayGlow(brew_overlay)
		end
	elseif id == 115294 then --mana tea
		if expirationTime > 0 then
			local startTime = expirationTime - duration
			tea_icon.cdtexture:SetCooldown(startTime, duration)
			tea_icon.cdtexture:Show()
			tea_icon:Show()
		end
		if count >= 6 then
			ActionButton_ShowOverlayGlow(tea_overlay)
		else
			ActionButton_HideOverlayGlow(tea_overlay)
		end
		tea_text:SetText(count)
	elseif id == 134563 then --healing elixir
		if not he_icon:IsShown() then
			he_icon:Show()
		end	
	elseif id == 137639 then --storm, earth, and fire
		if not sef_icon:IsShown() then
			sef_icon:Show()
		end	
		sef_text:SetText(count)
	end
end

function infMonk:Clear(id)
	if id == 125359 then --tiger power
		palm_icon.cdtexture:Hide()
		palm_icon:Hide()
	elseif id == 139597 then --muscle memory
		mm_icon.cdtexture:Hide()
		mm_icon:Hide()
	elseif id == 118674 then --vital mists
		ActionButton_HideOverlayGlow(vm_overlay)
		vm_icon.cdtexture:Hide()
		vm_icon:Hide()
	elseif id == 125195 then --tigereye brew
		ActionButton_HideOverlayGlow(brew_overlay)
		brew_icon.cdtexture:Hide()
		brew_icon:Hide()
	elseif id == 115294 then --mana tea
		ActionButton_HideOverlayGlow(tea_overlay)
		tea_icon.cdtexture:Hide()
		tea_icon:Hide()
	elseif id == 134563 then --healing elixir
		he_icon:Hide()
	elseif id == 137639 then --storm, earth, and fire
		sef_icon:Hide()		
	end
end

